﻿using CharmL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Demo {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void label8_Click(object sender, EventArgs e) {

        }

        private void Form1_Load(object sender, EventArgs e) {

        }

        private void btnBrowse_Click(object sender, EventArgs e) {
            openFileDialog1.Multiselect = false;
            openFileDialog1.Filter = "Text files|*.txt";
            DialogResult result = openFileDialog1.ShowDialog();

            if (result == DialogResult.OK) // Test result.
    	    {
                txtFileName.Text = openFileDialog1.FileName;
                lblTotatTrans.Text = File.ReadLines(txtFileName.Text).Count().ToString();
            }

        }

        private void btnRun_Click(object sender, EventArgs e) {
            if (string.IsNullOrEmpty(txtMinSup.Text)) {
                MessageBox.Show("Please enter minSup (%)!");
                return;
            }
            if (string.IsNullOrEmpty(txtInitSize.Text)) {
                MessageBox.Show("Please enter init size!");
                return;
            }
            if (string.IsNullOrEmpty(txtBatchSize.Text)) {
                MessageBox.Show("Please enter batch size!");
                return;
            }
            if (string.IsNullOrEmpty(txtDrescan.Text)) {
                MessageBox.Show("Please enter drescan!");
                return;
            }

            double minSup = int.Parse(txtMinSup.Text) * 1.0 / 100;
            int initSize = int.Parse(txtInitSize.Text);
            int batchSize = int.Parse(txtBatchSize.Text);
            int drescan = int.Parse(txtDrescan.Text);
            int fileSize = int.Parse(lblTotatTrans.Text);
            int su = (int)(minSup * fileSize);
            int sl = su - (fileSize - initSize);
            lblSl.Text = sl.ToString();
            lblSu.Text = su.ToString();

            //#region Mushroom test
            //string fileName = "mushrooms.txt";
            //int increBatchSize = 200;
            //int increSize = 50;
            //int fileSize = 8416;
            //double minSup = 0.25;


            //int increTotalSize = 400;

            //int ReadNumTrans = fileSize - increTotalSize;
            //#endregion
            string[] args = new string[] { };
            string fileName = txtFileName.Text;
            int increTotalSize = fileSize - initSize;
            Dictionary<int, long> charmL = OnlyCharmL(args, fileName, fileSize, minSup, batchSize, initSize);

            Dictionary<int, long> incre = TestIncre(args, fileName, drescan, batchSize, fileSize, minSup, increTotalSize, initSize);

            DataTable table = new DataTable();
            table.Columns.Add("#trans");
            table.Columns.Add("CHARM-L (ms)");
            table.Columns.Add("Proposed Algorithm (ms)");
            foreach (KeyValuePair<int, long> pair in charmL) {
                DataRow r = table.Rows.Add();
                r["#trans"] = pair.Key;
                r["CHARM-L (ms)"] = pair.Value;
                r["Proposed Algorithm (ms)"] = incre[pair.Key];
            }
            gvResult.DataSource = table;
        
        }

        private static Dictionary<int, long> OnlyCharmL(string[] args, string fileName, int fileSize, double minSup, int increSize, int ReadNumTrans) {
            Dictionary<int, long> dic = new Dictionary<int, long>();
            while (ReadNumTrans <= fileSize) {
                using (MyStopWatch sw = new MyStopWatch()) {
                    //DateTime start = DateTime.Now;
                    int minSupForCharm = (int)(fileSize * minSup);
                    Eclat obj = new Eclat();
                    obj.MINSUPPORT = minSupForCharm;
                    obj.READ_NUM_TRANS = ReadNumTrans;
                    obj.IS_INCRE = false;

                    obj.Run(args, fileName, "t1.txt", new Dictionary<int, long>());


                    Console.WriteLine("Trans " + ReadNumTrans + ".Time : ");
                    dic.Add(ReadNumTrans, sw.ElapsedMilliseconds);
                    ReadNumTrans += increSize;
                }
                Console.WriteLine("=================================================");
            }
            Console.WriteLine("=================================================");
            foreach (KeyValuePair<int, long> item in dic) {
                Console.WriteLine("Trans {0} Time {1}", item.Key, item.Value);
            }
            Console.WriteLine("=================================================");
            return dic;
        }

        private static Dictionary<int, long> TestIncre(string[] args, string fileName, int increBatchSize, int increSize, int fileSize, double minSup, int increTotalSize, int ReadNumTrans) {
            Dictionary<int, long> dic = new Dictionary<int, long>();
            ReadNumTrans = fileSize - increTotalSize;
            int minSupForCharm = (int)(fileSize * minSup) - increTotalSize;

            while (ReadNumTrans <= fileSize) {
                using (MyStopWatch sw = new MyStopWatch()) {

                    Eclat obj = new Eclat();
                    obj.MINSUPPORT = minSupForCharm;
                    obj.READ_NUM_TRANS = ReadNumTrans;// -increBatchSize;
                    obj.IS_INCRE = true;
                    obj.INCRE_BATCH_SIZE = increBatchSize;
                    obj.INCRE_SIZE = increSize;
                    Console.WriteLine("CHARM-L");
                    obj.Run(args, fileName, "t2.txt", dic);
                    Console.WriteLine("Trans {0} Nodes {1} Time ", ReadNumTrans, obj.maxtest.lattice.Count);
                }
                ReadNumTrans += increBatchSize;
                Console.WriteLine("=================================================");
                minSupForCharm += increBatchSize;

            }
            return dic;
        }
    }
}

